using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Toast Animator", 100)]
	public class ToastAnimator : MonoBehaviour
	{
		[SerializeField]
		protected Text m_Text;

		[SerializeField]
		protected RectTransform m_RectTransform;

		[SerializeField]
		protected Image m_PanelImage;

		[SerializeField]
		private CanvasGroup m_CanvasGroup;

		protected int m_State;

		protected bool m_AnimDown;

		protected Vector2 m_OnPos;

		protected Vector2 m_OffPos;

		protected Vector2 m_TempVec2;

		protected float m_TimeToWait;

		protected float m_AnimSpeed = 0.5f;

		protected float m_AnimStartTime;

		protected float m_AnimDeltaTime;

		protected float m_OffAlpha;

		protected float m_CurrentPosition;

		protected bool m_MoveFab;

		protected MaterialMovableFab m_MaterialMovableFab;

		protected RectTransform m_FabRectTransform;

		protected float m_FabStartPos;

		private void OnEnable()
		{
			Init();
			base.transform.localScale = new Vector3(1f, 1f, 1f);
			m_AnimStartTime = Time.realtimeSinceStartup;
			Vector3 position = m_RectTransform.position;
			m_CurrentPosition = position.y;
			m_State = 2;
		}

		public void Init()
		{
			m_OnPos = new Vector2((float)Screen.width / 2f, (float)Screen.height / 4f);
			m_OffPos = new Vector2((float)Screen.width / 2f, (float)Screen.height / 10f);
			m_CanvasGroup.alpha = 0f;
		}

		private void OnLevelWasLoaded()
		{
			m_CanvasGroup.alpha = 0f;
		}

		public void Show(Toast toast)
		{
			m_TimeToWait = toast.duration;
			m_Text.text = toast.content;
			m_PanelImage.color = toast.panelColor;
			m_Text.color = toast.textColor;
			m_Text.fontSize = toast.fontSize;
			Init();
			m_AnimStartTime = Time.realtimeSinceStartup;
			Vector3 position = m_RectTransform.position;
			m_CurrentPosition = position.y;
			m_State = 1;
		}

		private void Update()
		{
			m_AnimDeltaTime = Time.realtimeSinceStartup - m_AnimStartTime;
			if (m_State == 1)
			{
				if (m_AnimDeltaTime < m_AnimSpeed)
				{
					m_TempVec2 = m_RectTransform.position;
					m_TempVec2.y = Tween.CubeOut(m_CurrentPosition, m_OnPos.y, m_AnimDeltaTime, m_AnimSpeed);
					m_RectTransform.position = m_TempVec2;
					m_CanvasGroup.alpha = Tween.CubeInOut(m_CanvasGroup.alpha, 1f, m_AnimDeltaTime, m_AnimSpeed);
					if (m_MoveFab)
					{
						RectTransform fabRectTransform = m_FabRectTransform;
						Vector3 position = m_FabRectTransform.position;
						float x = position.x;
						float fabStartPos = m_FabStartPos;
						Vector3 position2 = m_RectTransform.position;
						float y = fabStartPos + (position2.y - m_OffPos.y);
						Vector3 position3 = m_FabRectTransform.position;
						fabRectTransform.position = new Vector3(x, y, position3.z);
					}
				}
				else
				{
					m_RectTransform.position = m_OnPos;
					if (m_MoveFab)
					{
						RectTransform fabRectTransform2 = m_FabRectTransform;
						Vector3 position4 = m_FabRectTransform.position;
						float x2 = position4.x;
						float fabStartPos2 = m_FabStartPos;
						Vector3 position5 = m_RectTransform.position;
						float y2 = fabStartPos2 + (position5.y - m_OffPos.y);
						Vector3 position6 = m_FabRectTransform.position;
						fabRectTransform2.position = new Vector3(x2, y2, position6.z);
					}
					StartCoroutine(WaitTime());
					m_State = 0;
				}
			}
			else
			{
				if (m_State != 2)
				{
					return;
				}
				if (m_AnimDeltaTime < m_AnimSpeed)
				{
					m_TempVec2 = m_RectTransform.position;
					m_TempVec2.y = Tween.CubeInOut(m_CurrentPosition, m_OffPos.y, m_AnimDeltaTime, m_AnimSpeed);
					m_RectTransform.position = m_TempVec2;
					m_CanvasGroup.alpha = Tween.CubeIn(m_CanvasGroup.alpha, m_OffAlpha, m_AnimDeltaTime, m_AnimSpeed);
					if (m_MoveFab)
					{
						RectTransform fabRectTransform3 = m_FabRectTransform;
						Vector3 position7 = m_FabRectTransform.position;
						float x3 = position7.x;
						float fabStartPos3 = m_FabStartPos;
						Vector3 position8 = m_RectTransform.position;
						float y3 = fabStartPos3 + (position8.y - m_OffPos.y);
						Vector3 position9 = m_FabRectTransform.position;
						fabRectTransform3.position = new Vector3(x3, y3, position9.z);
					}
				}
				else
				{
					if (m_MoveFab)
					{
						RectTransform fabRectTransform4 = m_FabRectTransform;
						Vector3 position10 = m_FabRectTransform.position;
						float x4 = position10.x;
						float fabStartPos4 = m_FabStartPos;
						Vector3 position11 = m_FabRectTransform.position;
						fabRectTransform4.position = new Vector3(x4, fabStartPos4, position11.z);
					}
					m_State = 0;
					OnAnimDone();
				}
			}
		}

		private IEnumerator WaitTime()
		{
			m_AnimDown = true;
			yield return new WaitForSeconds(m_TimeToWait);
			if (m_AnimDown)
			{
				m_AnimStartTime = Time.realtimeSinceStartup;
				Vector3 position = m_RectTransform.position;
				m_CurrentPosition = position.y;
				m_State = 2;
			}
		}

		protected virtual void OnAnimDone()
		{
			if (!ToastManager.OnToastCompleted())
			{
				UnityEngine.Object.Destroy(base.gameObject);
			}
		}
	}
}
